import os
from datetime import datetime, date, timedelta
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__, static_url_path="/task/static", static_folder="task/static")

app.config["SECRET_KEY"] = "morimatsu_jmo"
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///jmo.db'

app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://jmo:treenewbee@127.0.0.1/test"
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False


db = SQLAlchemy(app)


class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    no = db.Column(db.String(10), unique=True, index=True, nullable=False)
    name = db.Column(db.String(50), nullable=False)
    password_hash = db.Column(db.String(150), nullable=False)
    position = db.Column(db.String(50), nullable=True)
    delete = db.Column(db.Boolean, default=False)
    admin = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "No:{} Name:{} Password:{} Admin:{}".format(
            self.no, self.name, self.password_hash, self.admin
        )

    @property
    def password(self):
        return self.password_hash

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True


class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    color = db.Column(db.String(50), nullable=False)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Category:{}".format(self.name)


class Subcategory(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    color = db.Column(db.String(50), nullable=False)
    categoryid = db.Column(db.Integer, nullable=False)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Subcategory:{}".format(self.name)


class Jobnumber(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    desc = db.Column(db.String(150), nullable=False)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Job number:{}".format(self.name)


class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    remark = db.Column(db.Text, nullable=True)
    userid = db.Column(db.Integer, nullable=False)
    categoryid = db.Column(db.Integer, nullable=False)
    subcategoryid = db.Column(db.Integer, nullable=False)
    jobnumberid = db.Column(db.Integer, nullable=False)
    manhour = db.Column(db.Float, nullable=False)
    tasktime = db.Column(db.DateTime, nullable=False, default=datetime.now)
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.now)
    edittime = db.Column(db.DateTime)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Task:{}".format(self.name)


class Assignment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    remark = db.Column(db.Text, nullable=True)
    receiverid = db.Column(db.Integer, nullable=False)
    senderid = db.Column(db.Integer, nullable=False)
    jobnumberid = db.Column(db.Integer, nullable=False)
    startdate = db.Column(db.Date, nullable=False)
    enddate = db.Column(db.Date, nullable=False)
    taskrate = db.Column(db.Float, nullable=False, default=1)
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.now)
    edittime = db.Column(db.DateTime)
    delete = db.Column(db.Boolean, default=False)
    finish = db.Column(db.Boolean, default=False)
    grade = db.Column(db.Float)

    def __repr__(self):
        return "Assignment:{}".format(self.name)


class Holiday(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date, nullable=False)
    desc = db.Column(db.String(150), nullable=False)
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.now)
    edittime = db.Column(db.DateTime)
    delete = db.Column(db.Boolean, default=False)
    isworkday = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Holiday:{}".format(self.name)


class Attachment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    assignment_id = db.Column(db.Integer, nullable=False)
    file_name = db.Column(db.String(150), nullable=False)
    display_name = db.Column(db.String(150), nullable=False)

    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)
    edit_time = db.Column(db.DateTime)
    delete_time = db.Column(db.DateTime)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Attachment:{}".format(self.name)


class DepartmentOrg(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, nullable=False)
    child_id = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return "DepartmentOrg:{}".format(self.name)


class ProjectOrg(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, nullable=False)
    user_id = db.Column(db.Integer, nullable=False)
    is_manager = db.Column(db.Boolean, default=False)

    plan_manhour = db.Column(db.Float, nullable=True, default=0)

    def __repr__(self):
        return "ProjectOrg:{}".format(self.name)


class ProjectGrade(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, nullable=False)
    user_id = db.Column(db.Integer, nullable=False)
    year = db.Column(db.Integer, nullable=False)
    month = db.Column(db.Integer, nullable=False)
    grade = db.Column(db.Integer, nullable=False)
    assign_user_id = db.Column(db.Integer, nullable=False)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Grade:{}".format(self.name)


class DepartmentGrade(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False)
    year = db.Column(db.Integer, nullable=False)
    month = db.Column(db.Integer, nullable=False)
    grade = db.Column(db.Integer, nullable=False)
    assign_user_id = db.Column(db.Integer, nullable=False)
    delete = db.Column(db.Boolean, default=False)

    def __repr__(self):
        return "Grade:{}".format(self.name)


class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    userid = db.Column(db.Integer, nullable=False)
    createtime = db.Column(db.DateTime, nullable=False, default=datetime.now)
    edittime = db.Column(db.DateTime)
    delete = db.Column(db.Boolean, default=False)
    finish = db.Column(db.Boolean, default=False)
    tag = db.Column(db.String(10), nullable=False)

    def __repr__(self):
        return "Todo:{}".format(self.name)


if __name__ == "__main__":
    # db.drop_all()
    db.create_all()
    admin = User.query.filter_by(no="admin").first()
    # if admin:
    #     print(admin)
    #     admin.admin = True
    #     db.session.commit()
    # else:
    #     admin = User(no='admin', name='admin', password='jmoadmin',admin=True)
    #     user = User(no='3317', name='范文洁', password='1',admin=False)
    #     db.session.add(admin)
    #     db.session.add(user)
    #     db.session.commit()

    print("password 123456", admin.check_password("123456"))
    print("password admin", admin.check_password("admin"))
    print("password jmoadmin", admin.check_password("jmoadmin"))
    print("password jmo123456", admin.check_password("jmo123546"))

    # project_org1 = ProjectOrg(project_id=173,user_id=42, is_manager=True)
    # project_org2 = ProjectOrg(project_id=173,user_id=2)
    # db.session.add(project_org1)
    # db.session.add(project_org2)
    # db.session.commit()
